<?php
/**
 * Created by PhpStorm.
 * User: adminyj
 * Date: 2019/2/21
 * Time: 1:35
 */

namespace app\api\controller;


use app\common\controller\Base;
use app\api\model\Member;
use think\Db;

class Person extends Base
{

    /**
     * 密码修改
     * */
    public function codeChange()
    {

        $mobile = input('mobile');
        $type = input('type');
        if(!$mobile){
            $this->result('','-1','号码不得为空','json',[]);
        }

        $member = new Member();

        $code = rand('100000','999999');

        if($type == 1){
            $cacheName = "change_mobile_".$mobile;
        }else{
            $cacheName = "code_mobile_".$mobile;
        }

        $content = "【先锋港湾】提醒您：您的修改验证码：".$code."，打死也不要告诉别人哦！！有效期10分钟";

        Cache($cacheName."_code",$code,['expire'=>600]);
        Cache($cacheName,$mobile,['expire'=>600]);

        $res = $member->sendMessage($mobile,$content,'');

        libxml_disable_entity_loader(true);
        //先把xml转换为simplexml对象，再把simplexml对象转换成 json，再将 json 转换成数组。
        $value_array = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        if($value_array['returnstatus'] == "Success"){
            $this->result('','1','发送成功','json',[]);
        }else{
            $this->result('','-1','发送失败','json',[]);
        }


    }



    /**
     * 手机号修改
     * */
    public function changeMobile()
    {

        $code = input('code');
        $mobile = input('mobile');
        $token = input('token');
        $oldCode = cache("change_mobile_".$mobile."_code");

        if(!$code || !$mobile){
            $this->result('','-1','数据获取有误','json',[]);
        }

        if($code != $oldCode){
            $this->result('','-1','验证码错误','json',[]);
        }

        $userInfo = $this->check($token);

        if($userInfo['username'] == $mobile){
            $this->result('','-1','请勿重复绑定同个手机号','json',[]);
        }

        $data['mobile'] = $mobile;
        $data['username'] = $mobile;

        $res = Db::name('member')->where(['id' => $userInfo['uid']])->update($data);

        if($res){
            $this->result('','1','修改成功','json',[]);
        }else{
            $this->result('','-1','修改失败','json',[]);
        }

    }



    /**
     * 密码修改
     * */
    public function codeMobile()
    {

        $code = input('code');
        $mobile = input('mobile');

        $passWord = input('pass_word');
        $passWords = input('pass_word2');

        $token = input('token');
        $oldCode = cache("code_mobile_".$mobile."_code");

        if(!$code || !$mobile || !$passWord || !$passWords){
            $this->result('','-1','数据获取不完整','json',[]);
        }

        if($passWord != $passWords){
            $this->result('','-1','两次密码输入不完整','json',[]);
        }

        if($code != $oldCode){
            $this->result('','-1','验证码错误','json',[]);
        }

        $userInfo = $this->check($token);

        $data['password'] = md5($passWord);

        $res = Db::name('member')->where(['id' => $userInfo['uid']])->update($data);

        if($res || $res === 0){
            $this->result('','1','修改成功','json',[]);
        }else{
            $this->result('','-1','修改失败','json',[]);
        }

    }


    /**
     * 密码修改页面
     * */
    public function passWordIndex()
    {

        $token = input('token');
        $userInfo = $this->check($token);

        $mobile = $userInfo['username'];

        $data['mobile'] = $mobile;

        $this->result($data,'1','获取成功','json');

    }

}